<?php

/**
 * Image
 * Handle images class
 * @package 
 * @author 
 * @copyright 2010
 * @version $Id$
 * @access public
 */
class Image{

    var $image;
    var $imageType;
    var $originalFileName;
    
    function Image($fileNameImage)
    {
        $this->load($fileNameImage);
    }
    
    /**
     * Image::load()
     * Load image jpg, png or gif to manipulate.
     * @param string $filename
     * @return void
     */
    function load($fileNameImage)
    {
        if(!file_exists($fileNameImage)){
           die("The path file image \"$fileNameImage\" not exist.");
        }
           
        $image_info = getimagesize($fileNameImage);
        $type = $image_info[2];
        
        switch($type){
            case IMAGETYPE_JPEG:
                $image = imagecreatefromjpeg($fileNameImage);
            break;
            case IMAGETYPE_GIF:
                $image = imagecreatefromgif($fileNameImage);
            break;
            case IMAGETYPE_PNG:
                $image = imagecreatefrompng($fileNameImage);
            break;
            default:
                die("The image format is not valid jpg, png or gif.");
            break;
        }
        
        $this->originalFileName = $fileNameImage;
        $this->imageType = $type;
        $this->image = $image;
    }
    
    function save($fileNameImage="", $quality=75, $destroyResource=true, $permissions=null)
    {
        if(!$fileNameImage) $fileNameImage = $this->originalFileName;
        
        $imageType = $this->imageType;
        
        switch($imageType){
            case IMAGETYPE_JPEG:
                $b = imagejpeg($this->image, $fileNameImage, $quality);
            break; 
            case IMAGETYPE_GIF:
                $b = imagegif($this->image, $fileNameImage);
            break;
            case IMAGETYPE_PNG:
                $b = imagepng($this->image, $fileNameImage, $quality);
            break;
        }

        if($permissions != null){
            chmod($filename, $permissions);
        }
        
        if($destroyResource) $this->imageDestroy();
        return $b;
    }
            
    function output($quality=75, $destroyResource=true)
    {
        $imageType = $this->imageType;
        
        switch($imageType){
            case IMAGETYPE_JPEG:
                header("Content-type: image/jpeg");
                $b = imagejpeg($this->image, "", $quality);
            break; 
            case IMAGETYPE_GIF:
                header("Content-type: image/gif");
                $b = imagegif($this->image);
            break;
            case IMAGETYPE_PNG:
                header("Content-type: image/png");
                $b = imagepng($this->image, "", $quality);
            break;
        }
        
        if($destroyResource) $this->imageDestroy();
        return $b;
    }
    
    function getResource() 
    {
        return $this->image;
    }
    
    function getWidth()
    {
        return imagesx($this->image);
    }
    
    function getHeight()
    {
        return imagesy($this->image);
    }
    
    function resizeToHeight($height)
    {
        $ratio = $height / $this->getHeight();
        $width = $this->getWidth() * $ratio;
        $this->resize($width, $height);
    }
    
    function resizeToWidth($width)
    {
        $ratio = $width / $this->getWidth();
        $height = $this->getheight() * $ratio;
        $this->resize($width, $height);
    }
    
    function scale($scale)
    {
        $width = $this->getWidth() * $scale/100;
        $height = $this->getheight() * $scale/100; 
        $this->resize($width, $height);
    }
    
    function crop($widthCrop, $heightCrop, $x1=0, $y1=0, $dst_x=0, $dst_y=0)
    {
        $this->resize($widthCrop, $heightCrop, $x1, $y1, $dst_x, $dst_y, true);
    }
    
    private function resize($width, $height, $x1=0, $y1=0, $dst_x=0, $dst_y=0, $isCrop=false)
    {
        $new_image = imagecreatetruecolor($width, $height);
        imagecopyresampled($new_image, $this->image, $dst_x, $dst_y, $x1, $y1, $width, $height, 
            ($isCrop) ? $width : $this->getWidth(), ($isCrop) ? $height : $this->getHeight());
        imagedestroy($this->image);
        $this->image = $new_image;
    }
    
    /**
     * Image::imageDestroy()
     * Destroy an image resource.
     * @return bool
     */
    function imageDestroy()
    {
        $image = $this->image;
        $this->image = null;        
        return (is_null($image)) ? false : imagedestroy($image);
    }
}